
package ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import Bean.GetBankcardBean;
import event.DeleteCardEvent;
import widget.sideslip.BaseSwipeAdapter;
import widget.sideslip.DragEdge;
import widget.sideslip.ShowMode;
import widget.sideslip.SideslipSwipeItem;


/**
 * 银行卡适配器
 * Created by chenjian on 18/2/22.
 */
public class BankCardAdapter extends BaseSwipeAdapter {

    private Context mContext;
    private List<GetBankcardBean.DataBean.ItemsBean> mData = new ArrayList<>();

    public BankCardAdapter(Context context) {
        this.mContext = context;
        mContext = context;
    }

    public void setData(List<GetBankcardBean.DataBean.ItemsBean> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    public List<GetBankcardBean.DataBean.ItemsBean> getData() {
        return mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        return LayoutInflater.from(mContext).inflate(R.layout.item_card, parent, false);
    }

    @Override
    public void fillValues(final int position, View convertView) {
        final SideslipSwipeItem swipeItem = convertView.findViewById(R.id.swipe_item);
        LinearLayout layout = convertView.findViewById(R.id.lv_getbankcard_linear);
        TextView delete = convertView.findViewById(R.id.tumble_delete);
        TextView title = convertView.findViewById(R.id.lv_getbankcard_txt1);
        TextView content = convertView.findViewById(R.id.lv_getbankcard_txt3);

        if (mData.get(position).getDefaultCard() != 1) {
            swipeItem.setShowMode(ShowMode.PullOut);
            swipeItem.setDragEdge(DragEdge.Right);
            delete.setVisibility(View.VISIBLE);
        }else{
            delete.setVisibility(View.GONE);
        }
        if(mData.get(position).getDefaultCard() == 1){
            mData.get(position).setIsChecked(true);
        }
        title.setText(mData.get(position).getBank() + (mData.get(position).getDefaultCard() == 1 ? "（默认）" : ""));
        if(mData.get(position).getBankNo().toString().length()>4){
            content.setText("**** **** **** " + mData.get(position).getBankNo().substring(mData.get(position).getBankNo().toString().length() - 4, mData.get(position).getBankNo().toString().length()));

        }else{
            content.setText("**** **** **** ****");

        }

        delete.setOnClickListener(v ->
                EventBus.getDefault().post(new DeleteCardEvent(mData.get(position).getId(), position, mData.get(position).getDefaultCard())));
    }
}


